using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.ObjectBuilder;
using GC2.ExceptionHandler;
using GC2.ExceptionHandler.Services;
using GC2.Entities.Logistica;
using GC2.DataAccess;

namespace GC2.DataAccess.DALogistica
{
    public class DARemitosLDet
    {
        private Database _database;        // Objeto DataBase del Data Access Application Block

        #region Propiedades miembro
        /// <summary>
        /// Seteo la propiedad _database de la clase.
        /// </summary>
        private Database DataBase
        {
            get { return _database; }
            set { _database = value; }
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Contructor de la clase sin parametro. Crea un objeto DataBase
        /// del Data AppBlock tomando el defaultDatabase del app.config.
        /// </summary>
        //public AplicacionesDataAccess()
        //{
        //    this.DataBase = DatabaseFactory.CreateDatabase();
        //}
        #endregion

        #region DARemitosLDetInsert
        /// <summary>
        /// Inserta un detalle en la tabla RemitosLDet
        /// </summary>
        /// <param name="Connection"></param>
        /// <param name="RemitosLDet"></param>
        public void DARemitosLDetInsert(string Connection, RemitosLDet remitosLdet)
        {
            this.DataBase = DatabaseFactory.CreateDatabase(Connection);
            DbCommand cmd = DataBase.GetStoredProcCommand("RemitosLDet_Insert");

            try
            {
                using (cmd)
                {
                    DataBase.AddInParameter(cmd, "@Id_Remitos", DbType.Int64, remitosLdet.Id_Remitos);
                    DataBase.AddInParameter(cmd, "@ItemL", DbType.Int16, remitosLdet.ItemL);
                    DataBase.AddInParameter(cmd, "@IdHRuta", DbType.Int64, remitosLdet.IdHRuta);
                    DataBase.AddInParameter(cmd, "@Pagina", DbType.Int16, remitosLdet.Pagina);
                    DataBase.AddInParameter(cmd, "@Item", DbType.Int16, remitosLdet.Item);
                    DataBase.AddInParameter(cmd, "@Fecha", DbType.DateTime, remitosLdet.Fecha);
                    DataBase.AddInParameter(cmd, "@Origen", DbType.String, remitosLdet.Origen);
                    DataBase.AddInParameter(cmd, "@Remito", DbType.Int32, remitosLdet.Remito);
                    DataBase.AddInParameter(cmd, "@Embalaje", DbType.String, remitosLdet.Embalaje);
                    DataBase.AddInParameter(cmd, "@Cobro", DbType.String, remitosLdet.Cobro);
                    DataBase.AddInParameter(cmd, "@Destino", DbType.String, remitosLdet.Destino);
                    DataBase.AddInParameter(cmd, "@Destinatario", DbType.String, remitosLdet.Destinatario);
                    DataBase.AddInParameter(cmd, "@Kilos", DbType.Decimal, remitosLdet.Kilos);
                    DataBase.AddInParameter(cmd, "@Bultos", DbType.Decimal, remitosLdet.Bultos);
                    DataBase.AddInParameter(cmd, "@PorCobrar", DbType.Decimal, remitosLdet.PorCobrar);
                    DataBase.AddInParameter(cmd, "@IdUsuario", DbType.Int16, remitosLdet.IdUsuario);

                    DataBase.ExecuteNonQuery(cmd);
                }
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
        }
        #endregion

        #region DARemitosLDetGetAll
        /// <summary>
        /// Devuelve lista de RemitosLDet de una Hoja de Ruta
        /// </summary>
        /// <param name="Connection"></param>
        /// <param name="RemitosLDet"></param>
        /// <returns><List>RemitosLDet</List></returns>
        public List<RemitosLDet> DARemitosLDetGetAll(string Connection, RemitosLDet remitosLdet)
        {
            try
            {
                this.DataBase = DatabaseFactory.CreateDatabase(Connection);
                DbCommand cmd = DataBase.GetStoredProcCommand("RemitosLDet_GetAll");

                List<RemitosLDet> listaRemitosLDet = new List<RemitosLDet>();
                using (cmd)
                {
                    DataBase.AddInParameter(cmd, "@Id_Remitos", DbType.Int64, remitosLdet.Id_Remitos);

                    IDataReader Odbreader = DataBase.ExecuteReader(cmd);
                    while (Odbreader.Read())
                    {
                        RemitosLDet oRemitosLDet = new RemitosLDet();

                        oRemitosLDet.Id_Remitos = Convert.ToInt64(Odbreader["Id_Remitos"]);
                        oRemitosLDet.ItemL = (int)(Odbreader["ItemL"]);
                        oRemitosLDet.IdHRuta = Convert.ToInt64(Odbreader["IdHRuta"]);
                        oRemitosLDet.Pagina = (int)(Odbreader["Pagina"]);
                        oRemitosLDet.Item = (int)(Odbreader["Item"]);
                        oRemitosLDet.Fecha = (DateTime)Odbreader["Fecha"];
                        oRemitosLDet.Origen = (string)Odbreader["Origen"];;
                        oRemitosLDet.Remito = Convert.ToInt32(Odbreader["Remito"]);
                        oRemitosLDet.Embalaje = (string)Odbreader["Embalaje"];
                        oRemitosLDet.Cobro = (string)Odbreader["Cobro"];
                        oRemitosLDet.Destino = (string)Odbreader["Destino"];
                        oRemitosLDet.Destinatario = (string)Odbreader["Destinatario"];
                        oRemitosLDet.Kilos = (decimal)Odbreader["Kilos"];
                        oRemitosLDet.Bultos = (int)(Odbreader["Bultos"]);
                        oRemitosLDet.PorCobrar = (decimal)Odbreader["PorCobrar"];
                        oRemitosLDet.IdUsuario = (int)(Odbreader["IdUsuario"]);


                        oRemitosLDet.HojaRuta = oRemitosLDet.IdHRuta.ToString();
                        oRemitosLDet.HojaRuta += "/";
                        oRemitosLDet.HojaRuta += oRemitosLDet.Pagina.ToString();
                        if (oRemitosLDet.Item < 10)
                        {
                            oRemitosLDet.HojaRuta += " -  ";
                        }
                        else
                        {
                            oRemitosLDet.HojaRuta += " - ";
                        }
                        oRemitosLDet.HojaRuta += oRemitosLDet.Item.ToString();
                        oRemitosLDet.FFecha = oRemitosLDet.Fecha.ToString().Remove(11);

                        listaRemitosLDet.Add(oRemitosLDet);
                    }
                    Odbreader.Close();
                    Odbreader.Dispose();
                }
                return listaRemitosLDet;
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
        }
        #endregion

        #region DARemitosLDetDelete
        /// <summary>
        /// Borra todos los detalles en la tabla RemitosLDet de una Pagina de Hoja de Ruta
        /// </summary>
        /// <param name="Connection"></param>
        /// <param name="RemitosLDet"></param>
        public void DARemitosLDetDelete(string Connection, RemitosLDet remitosLdet)
        {
            this.DataBase = DatabaseFactory.CreateDatabase(Connection);
            DbCommand cmd = DataBase.GetStoredProcCommand("RemitosLDet_Delete");

            try
            {
                using (cmd)
                {
                    DataBase.AddInParameter(cmd, "@Id_Remitos", DbType.Int64, remitosLdet.Id_Remitos);

                    DataBase.ExecuteNonQuery(cmd);
                }
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
        }
        #endregion

    }
}

